//
//  CAnAlgRealMeasure.m
//  
//
//  Created by  on 2024/9/4.
//

#import "CAnAlgRealMeasure.h"

@implementation CAnAlgRealMeasure
- (instancetype)initWithData:(NSData *)data
{
    self = [super init];
    if (self) {
        if (data.length < 20) {
            return self;
        }
        uint8_t *dataBytes = (uint8_t *)[data bytes];
        
        if (dataBytes[2] != 0xFF) {
            _spo2 = @(dataBytes[2]);
        }
        if (dataBytes[3] != 0xFF) {
            _heartRate  = @(dataBytes[3]);
        }
        
        
        uint16_t tmpHrv = *((uint16_t *)(dataBytes +4));
        if (tmpHrv != 0xFFFF) {
            _hrv = @(tmpHrv);
        }
        
        _dataStatus = dataBytes[6];;

        _motion = *((uint16_t *)(dataBytes +7));

        /*hrQualityIndexValue :Heart rate quality index value: Range 1-100, when the value is 40 or above, it means the heart rate is accurate.
        ( 心率质量指标值(Quality index value): 范围 1-100，当该值为 40 或 40 以上时表示心率准确) */
        _hrQualityIndexValue  = dataBytes[9];

        if(dataBytes[10] != 0xFF) {
            _respirationRate = @(dataBytes[10]);
        }
        
        _spo2RValue = (*((uint32_t *)(dataBytes +11)))/100000.0f;

        uint16_t tmpIbi = *((uint16_t *)(dataBytes + 15));
        if (tmpIbi != 0xFFFF) {
            _ibi = @(tmpIbi);
        }
        
        uint stress = dataBytes[17] & 0xff;
        if (stress != 0xff) {
            _stress = @(stress / 100.0f);
        }
        
        if (dataBytes[18] <= 100) {
            _cardiacCoherence = @((dataBytes[18] & 0xff)/100.0f);
        }
        
   

        
    }
    return self;
}
@end
